var CommonProcessSetting = function () {
  var template = /* html */ `
    <div class="common-process-setting" v-loading.fullscreen.lock="loading">
      <div class="process-setting-btns">
        <el-button size="medium" type="primary" @click="addApproval">新增</el-button>
      </div>
      <div class="process-setting-table-div">
        <el-table fit stripe border :data="approvalList" style="width: 100%" height="parent">
          <el-table-column label="审批类型" min-width="100px" align="center">
            <template slot-scope="scope">
              <span>{{i18n.getMsg('attendance.approvalSetting.type.'+scope.row.type)}}</span>
            </template>
          </el-table-column>
          <el-table-column label="可审批人" prop="approvalName" min-width="150px" header-align="center">
          </el-table-column>
          <el-table-column label="操作" width="140px" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="updateApproval(scope.row)">修改</el-button>
              <el-button size="mini" type="danger" @click="deleteApproval(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div style="margin-top:10px;text-align:right;" v-if="pagination.totalCount!==0">
        <el-pagination background :page-size="pagination.pageSize" :total="pagination.totalCount" :current-page.sync="pagination.currentPage"
          @current-change="pageChange"></el-pagination>
      </div>

      <el-dialog :title="formType==='add'?'新增审批流程':'修改审批流程'" :visible.sync="openForm" width="540px" top="10vh" @closed="dialogClosed">
        <div style="height:calc(80vh - 54px);overflow:auto">
          <common-approval-form v-if="openForm" :type="formType" :approval="selectApproval" :dropdown-option="dropdownOption" @save-success="saveSuccess">
          </common-approval-form>
        </div>
      </el-dialog>
    </div>
  `;

  return {
    data: function () {
      return {
        loading: false,
        approvalList: [],
        dropdownOption: {},
        pagination: {
          currentPage: 1,
          totalCount: 0,
          pageSize: 10
        },
        openForm: false,
        formType: '',
        selectApproval: null,
        i18n: i18n
      };
    },

    created: function () {
      this.setApprovalList();
    },

    methods: {
      setApprovalList: function () {
        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;

        this.loading = true;
        var self = this;
        ajaxRequest('dingTalk/queryApprovalProcessSet', {
          type: '1,2,3',
          start: start,
          length: pageSize
        }, false).then(function (res) {
          var approvalList = res.returnData;
          if (approvalList.length === 0 && pagination.currentPage !== 1) {
            self.pagination.totalCount = res.iTotalRecords;
            self.$nextTick(function () {
              self.setApprovalList();
            });
            return;
          }

          self.approvalList = approvalList;
          self.pagination.totalCount = res.iTotalRecords;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      pageChange: function () {
        this.setApprovalList();
      },

      addApproval: function () {
        this.formType = 'add';
        this.openForm = true;
      },

      updateApproval: function (approval) {
        this.selectApproval = approval;
        this.formType = 'update';
        this.openForm = true;
      },

      deleteApproval: function (approval) {
        var self = this;
        this.$confirm('确定删除该审批流程设置？', '删除确认').then(function () {
          self.toDeleteApproval(approval);
        }).catch(function () {});
      },

      toDeleteApproval:function(approval){
        this.loading = true;
        var self = this;
        ajaxRequest('dingTalk/deleteApprovalProcessSet',{
          type:approval.type
        }).then(function(){
          self.loading = false;
          self.$alert('成功删除该审核流程设置','信息',{
            callback:function(){
              self.setApprovalList();
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      },

      saveSuccess: function () {
        this.openForm = false;
        this.setApprovalList();
      },

      dialogClosed: function () {
        this.formType = '';
        this.selectApproval = null;
      }
    },

    components: {
      CommonApprovalForm: CommonApprovalForm
    },

    template: template
  };
}();
